package com.cn.common;

import org.apache.commons.lang3.StringUtils;

public class DataSizeUtil {

    public static Long getDataBaseSize(String size) {
        if (StringUtils.isEmpty(size)) {
            //1G
            return 1024 * 1024 * 1024L;
        }
        size = size.toLowerCase();
        String length = size.substring(0, size.length() - 1);
        int length1 = Integer.valueOf(length);
        if (size.contains("g") || size.contains("G")) {
            return getSize(1) * length1;
        } else if (size.contains("m") || size.contains("M")) {
            return getSize(2) * length1;
        } else if (size.contains("k") || size.contains("K")) {
            return getSize(3) * length1;
        }

        return 1024 * 1024 * 1024L;
    }

    public static long getSize(int t) {
        if (t == 1) {
            return 1024 * 1024 * 1024;//1g
        } else if (t == 2) {
            return 1024 * 1024;//1m
        } else if (t == 3) {
            return 1024;//1k
        }
        return 1024 * 1024 * 1024;
    }

    public static void main(String[] args) {
        Integer t = getDataBaseSize("2g").intValue();
        System.out.println(t);
    }

}
